package com.ruoyi.system.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysTenantDB;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysPost;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cc
 * @ClassName: com.ruoyi.system.service.impl
 * @date: 2025/8/11 09:12
 * @Description:
 */
@Service
@Slf4j
public class InitTenantServiceImpl {

    @Resource
    private SysRoleServiceImpl sysRoleService;

    @Resource
    private SysDeptServiceImpl sysDeptService;

    @Resource
    private SysPostServiceImpl sysPostService;

    @Resource
    private SysUserServiceImpl sysUserService;



    // 入口
    public void init(SysTenantDB sysTenantDB){
        // 初始化 部门
        List<Long> deptIds = initDeptByCreateTenant(sysTenantDB);
        // 初始化 角色 最高权限
        Long highestAuthorityId = initRoleByCreateTenant(sysTenantDB.getId(), sysTenantDB.getTenantName(), deptIds);
        // 初始化 岗位,
        Long maxPostId = initPostByCreateTenant(sysTenantDB);
        // 初始化 用户
        initUserByCreateTenant(sysTenantDB, deptIds, highestAuthorityId, maxPostId);
    }

    // 初始化部门
    private List<Long> initDeptByCreateTenant(SysTenantDB sysTenantDB){
        Long maxId = sysDeptService.findMaxId();
        // 主键
        Long primaryId = maxId + 1L;

        SysDept sysDept = new SysDept();


        sysDept.setDeptId(primaryId);
        // 顶级父类部门的初始化
        sysDept.setParentId(0L);
        sysDept.setAncestors("0");
        sysDept.setDeptName(sysTenantDB.getTenantName());
        sysDept.setOrderNum(0);
        sysDept.setStatus(UserConstants.DEPT_NORMAL);
        sysDept.setLeader(sysTenantDB.getPrimaryName());
        sysDept.setPhone(sysTenantDB.getPrimaryPhone());
        sysDept.setTenantId(sysTenantDB.getId());


        sysDeptService.insertDept(sysDept);

        // 再创建一个子部门
        SysDept sysDeptChildren = getSysDeptChildren(sysTenantDB, primaryId);

        sysDeptService.insertDept(sysDeptChildren);

        return Arrays.asList(primaryId, sysDeptChildren.getDeptId());
    }



    // 初始化角色
    private Long initRoleByCreateTenant(Long tenantId, String tenantName, List<Long> deptIds){
        // 查询最大权限id
        Long maxId = sysRoleService.findMaxId();
        Long primaryId = maxId + 1L;

        SysRole sysRole = new SysRole();
        sysRole.setRoleId(primaryId);
        sysRole.setTenantId(tenantId);
        sysRole.setRoleName("系统管理员");
        sysRole.setRoleKey("system");
        sysRole.setRoleSort(1);
        sysRole.setDataScope("2");
        sysRole.setStatus("0");
        sysRole.setMenuCheckStrictly(Boolean.TRUE);
        sysRole.setDeptCheckStrictly(Boolean.TRUE);
        sysRole.setDeptIds(deptIds.toArray(new Long[0]));
        // 列表权限
        sysRole.setMenuIds(getDefaultSysMenuId().toArray(new Long[0]));

        // 插入 权限
        sysRoleService.insertRole(sysRole);
        // 插入权限关联表
        sysRoleService.insertRoleDept(sysRole);
        // 返回管理员id

        // 初始化 查询权限
        SysRole sysRole1 = new SysRole();
        sysRole1.setRoleId(primaryId + 1L);
        sysRole1.setTenantId(tenantId);
        sysRole1.setRoleName("仅查询角色");
        sysRole1.setRoleKey("onlyQuery");
        sysRole1.setRoleSort(2);
        sysRole1.setDataScope("2");
        sysRole1.setStatus("0");
        sysRole1.setMenuCheckStrictly(Boolean.TRUE);
        sysRole1.setDeptCheckStrictly(Boolean.TRUE);
        sysRole1.setDeptIds(deptIds.toArray(new Long[0]));
        // 列表权限
        sysRole1.setMenuIds(getFindMenuId().toArray(new Long[0]));

        sysRoleService.insertRole(sysRole1);
        sysRoleService.insertRoleDept(sysRole1);

        return primaryId;
    }

    // 初始化岗位(董事长,经理...)
    private Long initPostByCreateTenant(SysTenantDB sysTenantDB){
        Long maxId = sysPostService.findMaxId();

        // 董事长
        SysPost sysPost = new SysPost();
        sysPost.setPostId(maxId + 1L);
        sysPost.setPostCode("ceo");
        sysPost.setPostName("董事长");
        sysPost.setPostSort(1);
        sysPost.setStatus("0");
        sysPost.setTenantId(sysTenantDB.getId());

        // 项目经理, se
        SysPost sysPostChildren = new SysPost();
        sysPostChildren.setPostId(maxId + 2L);
        sysPostChildren.setPostCode("se");
        sysPostChildren.setPostName("项目经理");
        sysPostChildren.setPostSort(2);
        sysPostChildren.setStatus("0");
        sysPostChildren.setTenantId(sysTenantDB.getId());

        // 员工,user
        SysPost sysPostChildren2 = new SysPost();
        sysPostChildren2.setPostId(maxId + 3L);
        sysPostChildren2.setPostCode("user");
        sysPostChildren2.setPostName("员工");
        sysPostChildren2.setPostSort(3);
        sysPostChildren2.setStatus("0");
        sysPostChildren2.setTenantId(sysTenantDB.getId());

        // 插入
        sysPostService.insertPost(sysPost);
        sysPostService.insertPost(sysPostChildren);
        sysPostService.insertPost(sysPostChildren2);

        // 返回管理员的id
        return maxId + 1L;
    }

    // 通知初始化
    private void initNoticeByCreateTenant(SysTenantDB sysTenantDB){

    }

    // 初始化用户
    private void initUserByCreateTenant(SysTenantDB sysTenantDB, List<Long> deptIds,Long roleId, Long postId){
        Long maxId = sysUserService.findMaxId();

        // 创建租户管理员账号
        SysUser sysUser = new SysUser();
        sysUser.setUserId(maxId + 1L);
        sysUser.setDeptId(deptIds.get(0));
        sysUser.setUserName(sysTenantDB.getPrimaryName());
        sysUser.setNickName(sysTenantDB.getPrimaryName());
        sysUser.setPhonenumber(sysTenantDB.getPrimaryPhone());
        // 性别 未知
        sysUser.setSex("2");
        sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
        sysUser.setRemark("自动创建["+sysTenantDB.getTenantName()+"]管理员");
        sysUser.setTenantId(sysTenantDB.getId());
        sysUser.setTenantName(sysTenantDB.getTenantName());
        sysUser.setPostIds(new Long[]{postId});
        sysUser.setRoleIds(new Long[]{roleId});
        sysUserService.insertUser(sysUser);

        // 同时创建超级管理员
        SysUser sysUserSuper = new SysUser();
        sysUserSuper.setUserId(maxId + 2L);
        sysUserSuper.setDeptId(deptIds.get(0));
        sysUserSuper.setUserName("cc_"+sysTenantDB.getTenantName());
        sysUserSuper.setNickName("cc_"+sysTenantDB.getTenantName());
        sysUserSuper.setSex("2");
        sysUserSuper.setPassword(SecurityUtils.encryptPassword("admin123"));
        sysUserSuper.setRemark("系统创建的["+sysTenantDB.getTenantName()+"]超级管理员");
        sysUserSuper.setTenantId(sysTenantDB.getId());
        sysUserSuper.setTenantName(sysTenantDB.getTenantName());
        sysUserSuper.setPostIds(new Long[]{postId});
        sysUserSuper.setRoleIds(new Long[]{roleId});
        sysUserService.insertUser(sysUserSuper);

    }

    private static SysDept getSysDeptChildren(SysTenantDB sysTenantDB, Long primaryId) {
        Long secondaryId = primaryId + 1L;
        SysDept sysDeptChildren = new SysDept();
        sysDeptChildren.setDeptId(secondaryId);
        sysDeptChildren.setParentId(primaryId);
        String ancestors = 0 + "," + primaryId;
        sysDeptChildren.setAncestors(ancestors);
        sysDeptChildren.setDeptName("系统自带小区(请重新编辑)");
        sysDeptChildren.setOrderNum(1);
        sysDeptChildren.setLeader(sysTenantDB.getPrimaryName());
        sysDeptChildren.setPhone(sysTenantDB.getPrimaryPhone());
        sysDeptChildren.setTenantId(sysTenantDB.getId());
        sysDeptChildren.setStatus(UserConstants.DEPT_NORMAL);
        return sysDeptChildren;
    }

    private List<Long> getDefaultSysMenuId(){
        List<Integer> list = Arrays.asList(1,
                2,
                100,
                101,
                103,
                104,
                107,
                108,
                109,
                500,
                501,
                1000,
                1001,
                1002,
                1003,
                1004,
                1005,
                1006,
                1007,
                1008,
                1009,
                1010,
                1011,
                1016,
                1017,
                1018,
                1019,
                1020,
                1021,
                1022,
                1023,
                1024,
                1035,
                1036,
                1037,
                1038,
                1039,
                1040,
                1041,
                1042,
                1043,
                1044,
                1045,
                1046,
                1047,
                1048);

        return list.stream().map(item -> item.longValue()).collect(Collectors.toList());
    }

    private List<Long> getFindMenuId() {
        List<Integer> list = Arrays.asList(
                1,
                2,
                100,
                101,
                103,
                104,
                107,
                108,
                109,
                500,
                1000,
                1007,
                1016,
                1020,
                1035,
                1039,
                1046
        );
        return list.stream().map(item -> item.longValue()).collect(Collectors.toList());
    }
}
